package com.xl.array;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class ArrayDemo {
    public static void main(String[] args) {
        int [] array = new int[5];
        for (int i = 0; i < array.length; i++) {
            System.out.println("请输入您的工号：");
            Scanner scanner = new Scanner(System.in);
            int code = scanner.nextInt();
            array[i] = code;
        }

        System.out.println(Arrays.toString(array));

        //打乱数组顺序
        Random rng = new Random();
        for (int i = 0; i < array.length; i++) {
            //每遍历一个数组，都随机一个数组索引范围内的值，然后让当前遍历的值与该索引的值进行交换
            int index = rng.nextInt(array.length);
            //定义一个临时变量，用来保存当前遍历的值
            int temp = array[i];
            //把当前遍历的值与随机的值进行交换
            array[i] = array[index];
            //把随机的值赋给当前遍历的值
            array[index] = temp;
        }

        System.out.println(Arrays.toString(array));
    }
}
